package com.example.demo.design.jdkProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Objects;

public class ProxyFactory implements InvocationHandler {

    private TrainStation trainStation = new TrainStation();

    ISell getObject(){
        /**
         * ClassLoader loader：类加载器
         * Class<?>[] interfaces：被代理类实现的一些接口
         * InvocationHandler h：实现了 InvocationHandler 接口的对象
         */
        ISell proxyInstance = (ISell) Proxy.newProxyInstance(trainStation.getClass().getClassLoader(), trainStation.getClass().getInterfaces(), this);
        return proxyInstance;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object invoke = new Object();
        if(Objects.isNull(args)){
            System.out.println("卖票收取手续费");
            invoke = method.invoke(trainStation, args);
        }else if(args.length == 1){
            System.out.println("退票，想都别想");
            invoke = method.invoke(trainStation, args);
        }
        return invoke;
    }
}
